package com.atguigu.lease.web.admin.custom.config;

import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.custom.converter.StringToBaseEnumConverterFactory;
import com.atguigu.lease.web.admin.custom.converter.StringToItemTypeConverter;
import com.atguigu.lease.web.admin.interceptor.AuthenticationInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {
//    @Autowired
//    private StringToItemTypeConverter stringToItemTypeConverter;
//
//    /*注册stringToItemTypeConverter*/
//    @Override
//    public void addFormatters(FormatterRegistry registry) {
//        registry.addConverter(stringToItemTypeConverter);
//    }

    //    ----------使用转换器工厂统一设置String -> T extends BaseEnums类型转换-----
    @Autowired
    private StringToBaseEnumConverterFactory baseEnumConverterFactory;

    @Override
    /*在解析请求参数执行此方法*/
    public void addFormatters(FormatterRegistry registry) {
        /*注册转化器工厂*/
        registry.addConverterFactory(baseEnumConverterFactory);
    }

    @Autowired
    private AuthenticationInterceptor authenticationInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authenticationInterceptor)
                .addPathPatterns("/admin/**")
                .excludePathPatterns("/admin/login/**");
    }
}
